package dtu.app.listadapter;

import java.util.List;

import com.iqengines.demo.R;

import dtu.app.model.Category;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CategoryArrayAdapter extends ArrayAdapter<Category> {

	private final LayoutInflater mInflater;
	
	public CategoryArrayAdapter(Context context) {
		super(context, R.layout.category_list);
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	public void setData(List<Category> data) {
		clear();
		if (data != null) {
			for (Category c : data) {
				add(c);
			}
		}
	}
	
    /**
     * Populate new items in the list.
     */
    @Override public View getView(int position, View convertView, ViewGroup parent) {
        View view;
 
        if (convertView == null) {
            view = mInflater.inflate(R.layout.category_item, parent, false);
        } else {
            view = convertView;
        }
 
        Category item = getItem(position);
        ((TextView)view.findViewById(R.id.category_name)).setText(item.getName());
        ((ImageView)view.findViewById(R.id.category_image)).setImageResource(item.getImageResource());
        ((TextView)view.findViewById(R.id.category_count)).setText("" + item.getCount());
        
        // Select dairy products at startup.
        if (position == 0) {
        	view.setBackgroundColor(getContext().getResources().getColor(android.R.color.holo_blue_light));
        }
 
        return view;
    }

}
